import { Color4B, TextInputMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity';

export class TextInputWorkerMsgHandler {
    private static workerPort;

    static init(workerPort) : void {
        console.log("[cocos] textInput init Callback");
        TextInputWorkerMsgHandler.workerPort = workerPort;
    }

    static createTextInput(viewTag: number, x: number, y: number, w: number, h: number, paddingW: number, paddingH: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity( 'createTextInput', viewTag);
        let viewRect: ViewRect = new ViewRect(x, y, w, h);
        textInputMsgEntity.viewRect = viewRect;
        textInputMsgEntity.paddingW = paddingW;
        textInputMsgEntity.paddingH = paddingH;
        console.log("[cocos] message posted")
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static removeTextInput(viewTag: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('removeTextInput', viewTag);
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputViewRect(viewTag: number, x: number, y: number, w: number, h: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputViewRect', viewTag);
        let viewRect: ViewRect = new ViewRect(x, y, w, h);
        textInputMsgEntity.viewRect = viewRect;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputVisible(viewTag: number, visible: boolean) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputVisible', viewTag);
        textInputMsgEntity.visible = visible;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputBold(viewTag: number, isBold: boolean) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputBold', viewTag);
        textInputMsgEntity.isBold = isBold;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputEditable(viewTag: number, editable: boolean) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputEditable', viewTag);
        textInputMsgEntity.editable = editable;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setCurrentText(viewTag: number, text: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setCurrentText', viewTag);
        textInputMsgEntity.text = text;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputFontSize(viewTag: number, size: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputFontSize', viewTag);
        textInputMsgEntity.fontSize = size;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputFontColor(viewTag: number, r: number, g: number, b: number, a: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputFontColor', viewTag);
        let color: Color4B = new Color4B(r, g, b, a);
        textInputMsgEntity.color = color;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputPlaceHolderFontColor(viewTag: number, r: number, g: number, b: number, a: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputPlaceHolderFontColor', viewTag);
        let color: Color4B = new Color4B(r, g, b, a);
        textInputMsgEntity.placeHolderColor = color;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputPlaceHolderFontSize(viewTag: number, size: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputPlaceHolderFontSize', viewTag);
        textInputMsgEntity.placeHolderSize = size;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputPlaceHolder(viewTag: number, text: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputPlaceHolder', viewTag);
        textInputMsgEntity.placeHolderText = text;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputMaxLength(viewTag: number, maxLength: number) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputMaxLength', viewTag);
        textInputMsgEntity.maxLength = maxLength;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputAlign(viewTag: number, align: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputAlign', viewTag);
        textInputMsgEntity.align = align;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setNativeInputMode(viewTag: number, inputMode: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setNativeInputMode', viewTag);
        textInputMsgEntity.inputMode = inputMode;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setNativeInputFlag(viewTag: number, isSecure: boolean) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity( 'setNativeInputFlag', viewTag);
        textInputMsgEntity.isSecure = isSecure;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setNativeReturnKey(viewTag: number, enterKey: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity( 'setNativeReturnKey', viewTag);
        textInputMsgEntity.enterKey = enterKey;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputFontPath(viewTag: number, fontPath: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputFontPath', viewTag);
        textInputMsgEntity.fontPath = fontPath;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static setTextInputPlaceHolderFontPath(viewTag: number, fontPath: string) : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('setTextInputPlaceHolderFontPath', viewTag);
        textInputMsgEntity.placeHolderFontPath = fontPath;
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }

    static hideAllTextInput() : void {
        let textInputMsgEntity: TextInputMsgEntity = new TextInputMsgEntity('hideAllTextInput');
        TextInputWorkerMsgHandler.workerPort.postMessage(textInputMsgEntity);
    }
}